<?php
//----------------------------------------------PRODUK
function edit_bcktokolink($config,$param){
	$link = trim($param['link']);
	$db = new db($config);
	$que = "UPDATE `footer` SET `online`=\"$link\" WHERE `id` = 1;";
	$db->exec($que);
	$_SESSION['editted'] = 'Sukses edit link toko online';
	header('Location: '.$config['home_url']."backend/produk");
}

function edit_bcktokokontak($config,$param){
	$notelp = trim($param['notelp']);
	$email = trim($param['email']);
	$alamat = trim($param['alamat']);
	var_dump($param);
	$db = new db($config);
	$que = "UPDATE `kontak` SET `telp`=\"$notelp\", `email`=\"$email\", `alamat`=\"$alamat\" WHERE `page` = 'toko';";
	$db->exec($que);
	$_SESSION['editted'] = 'Sukses edit kontak toko';
	header('Location: '.$config['home_url']."backend/produk");
}

function add_bcktokoprod($config,$param){
	$uploaddir = $config['image_path'].'produk'.DIRECTORY_SEPARATOR.'produk'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'produk'.DIRECTORY_SEPARATOR.'pre-prod'.DIRECTORY_SEPARATOR;
	$limitedext = array("jpg","jpeg","png","bmp");
	$ext = strrchr($_FILES['gambar']['name'],'.');
	$ext = strtolower($ext);
	$getExt = explode('.',$ext);
	$getName = explode('.',$_FILES['gambar']['name']);
	$getName = helper::slugging($getName[0]);
	$file_ext = $getExt[count($getExt)-1];
	$file_name = $getName.".".$file_ext;
	$uploadfile = $uploaddir.$file_name;
	if (!in_array($file_ext,$limitedext)) {
		$_SESSION['editted'] = "Tipe file salah<br />Tipe file yang diperbolehkan adalah jpg, jpeg, bmp, png.";
	} else {
		if (move_uploaded_file($_FILES['gambar']['tmp_name'], $uploadfile)) {
			// process for thumbnail image
			WideImage::load($uploadfile)->resize(500,300,'fill')->saveToFile($uploadfile);	//produk
			WideImage::load($uploadfile)->resize(100,60,'fill')->saveToFile($preddir.$file_name);	//pre
			//WideImage::load($uploadfile)->crop('center','center',250,300)->saveToFile($preddir.$file_name.'.'.$file_ext);	//pre
			$_SESSION['editted'] = "Sukses upload gambar produk";
		} else {
			switch ($_FILES['gambar']['error']) {
				case 2:
					$_SESSION['editted'] = "Error: Ukuran gambar melebihi batas<br />Batas ukuran file adalah 1 MB";
					break;
				case 7:
					$_SESSION['editted'] = "Error: Tidak dapat disimpan dalam server";
					break;
				default:
					$_SESSION['editted'] = "Error: Unknown error ".$_FILES['gambar']['error'];
			}
		}
	}
	header('Location: '.$config['home_url']."backend/produk");
}

function del_bcktokoprod($config,$param){
	$img=$param['img'];
	$uploaddir = $config['image_path'].'produk'.DIRECTORY_SEPARATOR.'produk'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'produk'.DIRECTORY_SEPARATOR.'pre-prod'.DIRECTORY_SEPARATOR;
	unlink($uploaddir.$img);
	unlink($preddir.$img);
	$_SESSION['editted'] = 'Sukses hapus foto produk';
	header('Location: '.$config['home_url']."backend/produk");
}

function add_bcktokospon($config,$param){
	$uploaddir = $config['image_path'].'produk'.DIRECTORY_SEPARATOR.'sponsor'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'produk'.DIRECTORY_SEPARATOR.'pre-spon'.DIRECTORY_SEPARATOR;
	$limitedext = array("jpg","jpeg","png","bmp");
	$ext = strrchr($_FILES['gambar']['name'],'.');
	$ext = strtolower($ext);
	$getExt = explode('.',$ext);
	$getName = explode('.',$_FILES['gambar']['name']);
	$getName = helper::slugging($getName[0]);
	$file_ext = $getExt[count($getExt)-1];
	$file_name = $getName.".".$file_ext;
	$uploadfile = $uploaddir.$file_name;
	if (!in_array($file_ext,$limitedext)) {
		$_SESSION['editted'] = "Tipe file salah<br />Tipe file yang diperbolehkan adalah jpg, jpeg, bmp, png.";
	} else {
		if (move_uploaded_file($_FILES['gambar']['tmp_name'], $uploadfile)) {
			// process for thumbnail image
			WideImage::load($uploadfile)->resize(500,300,'fill')->saveToFile($uploadfile);	//sponsor
			WideImage::load($uploadfile)->resize(100,60,'fill')->saveToFile($preddir.$file_name);	//pre
			//WideImage::load($uploadfile)->crop('center','center',250,300)->saveToFile($preddir.$file_name.'.'.$file_ext);	//pre
			$_SESSION['editted'] = "Sukses upload gambar produk";
		} else {
			switch ($_FILES['gambar']['error']) {
				case 2:
					$_SESSION['editted'] = "Error: Ukuran gambar melebihi batas<br />Batas ukuran file adalah 1 MB";
					break;
				case 7:
					$_SESSION['editted'] = "Error: Tidak dapat disimpan dalam server";
					break;
				default:
					$_SESSION['editted'] = "Error: Unknown error ".$_FILES['gambar']['error'];
			}
		}
	}
	header('Location: '.$config['home_url']."backend/produk");
}

function del_bcktokospon($config,$param){
	$img=$param['img'];
	$uploaddir = $config['image_path'].'produk'.DIRECTORY_SEPARATOR.'sponsor'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'produk'.DIRECTORY_SEPARATOR.'pre-spon'.DIRECTORY_SEPARATOR;
	unlink($uploaddir.$img);
	unlink($preddir.$img);
	$_SESSION['editted'] = 'Sukses hapus foto produk';
	header('Location: '.$config['home_url']."backend/produk");
}
